﻿using System;
using System.Collections.Generic;

namespace Aeon.Framework
{

/// <summary>
/// Input manager class.
/// Manages contextualized input handling.
/// 
/// </summary>
public sealed class AeonInputManager : IInputManager
    {
    // --- Attributes:

    private List<InputContext> contexts;        // Created contexts.
    private InputContext active;                // Current context.

    // --- Constructor:

    public AeonInputManager()
        {

        CleanInputs();
        }

    // --- Public Methods:

    /// <summary>
    /// Notifies active context of input event.
    /// 
    /// </summary>
    public void Notify(String input)
        {

        if (active != null) active.Notify(input);
        }

    /// <summary>
    /// Chooses active context for input handling.
    /// 
    /// </summary>
    public bool MakeActive(String name)
        {
        bool activated = false;

        foreach(InputContext inp in contexts)
            {
            if (inp.Name == name)
                {
                activated = true;
                active = inp;
                break;
                }
            }

        return(activated);
        }

    /// <summary>
    /// Gets existing context, or creates a new one.
    /// 
    /// </summary>
    public InputContext GetInputs(String name)
        {
        InputContext search = null;

        foreach(InputContext inp in contexts)
            {
            if (inp.Name == name)
                {
                search = inp;
                break;
                }
            }

        if (search == null)
            {
            search = new InputContext(name);
            contexts.Add(search);
            }

        return(search);
        }

    /// <summary>
    /// Clears all input context handlers.
    /// 
    /// </summary>
    public void CleanInputs()
        {
        if (contexts != null) contexts.Clear();
        else contexts = new List<InputContext>();

        active = null;
        }

    }


}
